import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-03-11
 * Time:0:04
 */
@WebServlet("/show")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        //调用上述api，把得到的结果构造成一个字符串，统一返回给客户端
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getContextPath());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");

        //获取所有的header
        Enumeration<String> headerNames= req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String key=headerNames.nextElement();
            String value=req.getHeader(key);
            stringBuilder.append(key+":"+value+"<br>");
        }
        //告诉浏览器，数据是啥类型，任何一次服务器返回都应该做的事。
        resp.setContentType("text/html;charset=utf8");
        //把上述内容返回给客户端这边;
        resp.getWriter().write(stringBuilder.toString());
        System.out.println("hello");
    }
}